#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sqlite3.h>
#include"parse.h"
#include"stringhandler.h"


//GLOBAL FILE NAME
char *file ="statement.txt";

//SELECT DATA AND PRINT
char* select_data(char *arg1){
	if(*arg1 == '*'){
	snprintf(select_buffer,sizeof(select_buffer),"%s%s%s%s","SELECT ",arg1," FROM "," COMPANY ");//only example
	}else{
		snprintf(select_buffer,sizeof(select_buffer),"%s%s%s%s%s%s%s%s%s","SELECT ","* ","FROM ","COMPANY ","WHERE ", "NAME=","'",arg1,"'");//places for variables
	}
	return &(select_buffer[0]);
}
//CREATE TABLE
char *create_table(){
	char* sql;
	sql = read_data(file);//gets string from parser.
	return sql;
}
//INSERT DATA TO DB
char* insert_data(char* NAME,int AGE,char* ADDRESS,float SALARY){
		snprintf(insert_buffer,sizeof(insert_buffer),"%s%s%s%s%s%s%d%s%s%s%s%s%f%s","INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) ","VALUES (null,","'",NAME,"'",",",AGE,",","'",ADDRESS,"'",",",SALARY,");");

	return &(insert_buffer[0]);

}
//UPDATE DATA
char* update_data(char *TABLE,char* FIELD,char* VALUE,int ID){
	snprintf(update_buffer,sizeof(update_buffer),"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s","UPDATE ","'",TABLE,"'"," set","'",FIELD,"'","=","'",VALUE,"'","where ID=","'",ID,"'",";");
	return &(update_buffer[0]);
}
//DELETE DATA
char* delete_data(char* TABLE,int ID){
	snprintf(delete_buffer,sizeof(delete_buffer),"%s%s%s%s%s%s%d%s%s","DELETE from ","'",TABLE,"'"," where ID=","'",ID,"'",";");
	return &(delete_buffer[0]);
}
void gui(){
	printf("1. create table\n");
	printf("2. insert data\n");
	printf("3. print data\n");
	printf("4. update data\n");
	printf("5. delete data\n");
}
//for test purposes only
char* collect_delete_data(){
	char* delete_res;
	char *TABLE;
	int ID;
	printf("Enter table name\n");
	//scanf("%s",TABLE);
	TABLE = "COMPANY";
	printf("Enter ID to delete wanted row\n");
	scanf("%d",&ID);
		delete_res = delete_data(TABLE,ID);
	
	return &(delete_res[0]);
}


//for test purposes only
char* collect_update_data(){
	char* update_res;
	char *TABLE;
	char FIELD[24];
	char VALUE[24];
	int ID;
	printf("Please select what you want to update\n");
	printf("Give table name\n");
	//scanf("%s",TABLE);
	TABLE = "COMPANY";
	printf("Give field\n");
	printf("example: NAME\n");
	scanf("%s",FIELD);
	printf("Give new value\n");
	scanf("%s",VALUE);
	printf("Give ID which row to update\n");
	scanf("%d",&ID);
		update_res = update_data(TABLE,FIELD,VALUE,ID);

	return &(update_res[0]);
}
//test purposes only
char* collect_user_data(){
	char *inject;
	char NAME[24];
	int AGE;
	char ADDRESS[24];
	float SALARY;
		//maybe own string builder
		printf("Give name\n");
		scanf("%s",NAME);
		printf("Give age\n");
		scanf("%d",&AGE);
		printf("Give address\n");
		scanf("%s",ADDRESS);
		printf("Give salary\n");
		scanf("%f",&SALARY);
	
			inject = insert_data(NAME,AGE,ADDRESS,SALARY);
	return &(inject[0]);
}
